import store from "../redux/store";
// 是否是移动端
export const isMobile = () => {
    const userAgent = navigator.userAgent.toLowerCase();
    const isMobile = /mobile/i.test(userAgent) || window.innerWidth <= 1080;
    return isMobile;
}

// 本地缓存
export function storageData(key, data) {
    let dataInfo;
    try {
        if (data) {
            localStorage.setItem(key, JSON.stringify(data));
        } else {
            dataInfo = JSON.parse(localStorage.getItem(key));
        }
    } catch (err) {
       throw Error(err)
    }
    return dataInfo;
}

// 提示
export const showToast = (child, time = 1500) =>  {
    const toast = document.createElement('div');
    if (typeof child === 'string') {
        toast.innerText = child;
    } else {
        throw new Error('toast参数错误');
    }
    toast.style.position = 'fixed';
    toast.style.top = isMobile() ? '50%' : '5%';
    toast.style.left = '50%';
    toast.style.transform = 'translate(-50%, -50%)';
    toast.style.backgroundColor = '#000';
    toast.style.color = '#fff';
    toast.style.padding = '10px 20px';
    toast.style.borderRadius = '5px';
    toast.style.zIndex = '9999';
    document.body.appendChild(toast);
    setTimeout(() => {
        document.body.removeChild(toast);
    }, time);
}

/**
 * 判断当前的页面是否激活
 * @param {*} refDom 
 * @returns 
 */
export const isDomActive = (refDom) => {
    const {page_index} = store.getState()?.job;
    let index = refDom.current && refDom.current.getAttribute('data-index');
    return page_index == index
}



/**
 * 是否缩小页面，因为有的页面切换成英语，文字较多，为了兼容页面
 * @param {*} num 缩小的基数
 * @param {*} styles 样式
 * @returns 
 */
export const isShrinkPage = (num = 800, styles={}) => {
    const {language} = store.getState()?.global;
    let pageHeight = document.body.clientHeight;
    let isActive = isMobile() && pageHeight / num <= 1
    return {
        transform: isActive ? `scale(${pageHeight / num})` : 'scale(1)',
        ...(isActive ? styles : {})
    } 
}